#include "DashboardUI_AddRecord.h"
#include "ui_dashboard_addrecord.h"

#include "Authentication.h"

#include "InfoBoxUI.h"
#include "RecordDatabase.h"

DashboardUI_AddRecord::DashboardUI_AddRecord(QWidget *parent) :
    QWidget(parent),
    validator(this),
    ui(new Ui::DashboardUI_AddRecord)
{
    ui->setupUi(this);

    Authentication * auth = Authentication::getInstance();
    User * user = auth->getUser();

    if (user->getUserType() == User::DOCTOR)
        setCommentOnly();
    else
    {
        // Hide record selection combo box
        ui->recordComboBox->hide();

        // Set up field validator
        validator.addAllFields();
        validator.setButton(ui->submitButton);
    }
}

DashboardUI_AddRecord::~DashboardUI_AddRecord()
{
    delete ui;
}

void DashboardUI_AddRecord::reload()
{
    loadRecords();
}

void DashboardUI_AddRecord::loadCurrentRecord(int)
{
    // Get record id of selected record
    int index = ui->recordComboBox->currentIndex();
    int recordId = ui->recordComboBox->itemData(index).toInt();

    // Load record
    RecordDatabase recordDB;
    Record rec = recordDB.getRecordById(recordId);

    // Set fields to record
    ui->bloodPressureDiaField->setText(QString::number(rec.bloodPressureDiastolic));
    ui->bloodPressureSysField->setText(QString::number(rec.bloodPressureSystolic));
    ui->commentField->setPlainText(rec.commentDoctor);
    ui->pulseField->setText(QString::number(rec.pulseRate));
    ui->respRateField->setText(QString::number(rec.respiratoryRate));
    ui->temperatureField->setText(QString::number(rec.temperature));
}

void DashboardUI_AddRecord::on_submitButton_clicked()
{
    Authentication * auth = Authentication::getInstance();
    User *user = auth->getUser();

    int id;

    if (user->getUserType() == User::DOCTOR)
    {
        addComment();
    }
    else if (user->getUserType() == User::NURSE)
    {
        if (auth->getPatient() != nullptr)
        {
            id = auth->getPatient()->getId();
            addRecord(id, false);
        }
    }
    else
    {
        id = user->getId();
        addRecord(id, true);
    }
}

void DashboardUI_AddRecord::addRecord(int patientId, bool patientComment)
{
    // Create record from input fields
    Record record;
    record.bloodPressureDiastolic = ui->bloodPressureDiaField->text().toInt();
    record.bloodPressureSystolic = ui->bloodPressureSysField->text().toInt();
    record.pulseRate = ui->pulseField->text().toInt();
    record.temperature = ui->temperatureField->text().toFloat();
    record.observationDate = QDateTime::currentDateTime();

    if (patientComment)
        record.commentPatient = ui->commentField->toPlainText();
    else
        record.commentDoctor = ui->commentField->toPlainText();

    // Add record to database
    RecordDatabase recordDB;
    recordDB.addRecord(&record, patientId);

    new InfoBoxUI("Record submitted", 1500, this);
    clearFields();

    emit recordsChanged();
}

void DashboardUI_AddRecord::addComment()
{
    // Get comment from text field
    QString comment = ui->commentField->toPlainText();

    // Get record id of selected record
    int index = ui->recordComboBox->currentIndex();
    int recordId = ui->recordComboBox->itemData(index).toInt();

    // Add comment to selected record
    RecordDatabase recordDB;
    recordDB.addComment(comment, recordId);

    new InfoBoxUI("Comment submitted", 1500, this);
    ui->commentField->clear();

    emit recordsChanged();
}

void DashboardUI_AddRecord::clearFields()
{
    validator.resetFields();

    ui->commentField->clear();
}

void DashboardUI_AddRecord::setCommentOnly()
{
    connect(ui->recordComboBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(loadCurrentRecord(int)));

    // Disable entry fields
    QList<QLineEdit *> lineEditList = this->findChildren<QLineEdit *>();
    foreach (QLineEdit * line, lineEditList)
    {
        line->setEnabled(false);
    }

    loadRecords();
}

void DashboardUI_AddRecord::loadRecords()
{
    Authentication * auth = Authentication::getInstance();
    User * patient = auth->getPatient();

    // If there is no patient selected, there are no records to show
    if (patient == nullptr)
        return;

    // Get all records of current user to update record selection combo box
    RecordDatabase recordDB;
    recordDB.loadRecords(patient->getId());
    QList<Record> * records  = recordDB.getRecords();

    // Update combobox with dates of each record
    ui->recordComboBox->clear();
    foreach (Record r, *records)
    {
        ui->recordComboBox->addItem(r.observationDate.toString(), QVariant(r.recordId));
    }
}
